import { createCheckFlowNode, updateCheckFlowNode } from '@/api/basic'
import { ref } from 'vue'

/**
 * 审批流程节点表单服务
 */
export default function () {
  const checkFlowNodeForm = ref<BaseInfoAPIRequest.CheckFlowNodeForm>({
    approvalOrRejectRoleId: '',
    approvalOrRejectUserId: '',
    gmtCreate: '',
    gmtUpdate: '',
    id: '',
    nodeName: '',
    empIds: [],
  })
  const checkFlowNodeFormSaveLoading = ref(false)

  const resetCheckFlowNodeForm = () => {
    checkFlowNodeForm.value = {
      approvalOrRejectRoleId: '',
      approvalOrRejectUserId: '',
      gmtCreate: '',
      gmtUpdate: '',
      id: '',
      nodeName: '',
      empIds: [],
    }
  }

  const checkFlowNodeFormSave = async (nodeId?: string) => {
    try {
      checkFlowNodeFormSaveLoading.value = true
      let res: APIResponse = {
        code: 0,
        message: '',
        success: false,
        data: {
          row: {},
        },
        hasOtherCode: false,
        otherCodePrefix: null,
      }

      if (nodeId) {
        checkFlowNodeForm.value.id = nodeId
        res = (await updateCheckFlowNode(checkFlowNodeForm.value)).data
      } else {
        res = (await createCheckFlowNode(checkFlowNodeForm.value)).data
      }

      checkFlowNodeFormSaveLoading.value = false
      return res
    } catch (error) {
      checkFlowNodeFormSaveLoading.value = false
    }
  }

  return {
    checkFlowNodeFormSaveLoading,
    checkFlowNodeForm,
    resetCheckFlowNodeForm,
    checkFlowNodeFormSave,
  }
}
